﻿#ifndef __TINYXMLHELPER__H__
#define __TINYXMLHELPER__H__
#include "tinyxml2.h"

__inline void copyElement(tinyxml2::XMLElement* dst, tinyxml2::XMLElement* src)
{
    if (NULL == dst || NULL == src)
    {
        return;
    }

    if (NULL != src->Name())
    {
        dst->SetName(src->Name());
    }
    if (NULL != src->GetText())
    {
        dst->SetText(src->GetText());
    }

    const tinyxml2::XMLAttribute* attr = src->FirstAttribute();
    while (NULL != attr)
    {
        dst->SetAttribute(attr->Name(), attr->Value());

        attr = attr->Next();
    }


    tinyxml2::XMLElement* element = src->FirstChildElement();
    while (NULL != element)
    {
        tinyxml2::XMLElement* elm = dst->GetDocument()->NewElement("");
        copyElement(elm, element);
        dst->InsertEndChild(elm);

        element = element->NextSiblingElement();
    }

}

__inline tinyxml2::XMLElement* xmlCreateNewNode(tinyxml2::XMLDocument& doc, char* xmlstr, int len)
{
    tinyxml2::XMLDocument doc1;
    if (tinyxml2::XML_SUCCESS != doc1.Parse(xmlstr, len)) {
        return NULL;
    }

    tinyxml2::XMLElement* elment = doc1.RootElement();
    if (NULL == elment) {
        return NULL;
    }

    tinyxml2::XMLElement* dst = doc.NewElement("");

    copyElement(dst, elment);
    return dst;
}

#endif
